export class Config {
	constructor() {
		this.header = {
			'content-type': 'application/json'
		};
		this.baseUrl = ''
		if (process.env.NODE_ENV === 'development') {
			this.baseUrl="http://localhost:8083"
		} else {
			this.baseUrl="http://localhost:8083"
		}
	}
	// 在其内部设置请求响应拦截
	request(
		url,
		method,
		data = {},
		header=this.header
	) {
		// 看需求配置是否是登录状态
		return new Promise((resolve,reject)=>{
			uni.request({
				url:this.baseUrl+url,
				method,
				data,
				header,
				success(res){
					resolve(res)
				},
				fail(error){
					reject(error)
				}
			})
		})
	}
	get(url,data,header) {
		return this.request(url,"get",data,header)
	}
	post(url,data,header) {
		return this.request(url,"post",data,header)
	}
	put(url, data, header) {
	    return this.request(url, 'PUT', data, header);
	}
	delete(url, data, header) {
	    return this.request(url, 'DELETE', data, header);
	}
	uploadFile(imgUrl) { // 文件上传
	    return new Promise((resolve, reject) => {
	      wx.uploadFile({
	        url: this.baseURL + 'file/uploadFile',
	        filePath: imgUrl,
	        name: 'file',
	        header: {
	          'content-type': 'multipart/form-data'
	        },
	        success: (res) => {
	          let data = JSON.parse(res.data);
	
	          if (data.code === '200') {
	            resolve(data);
	          } else {
	            reject(data);
	          }
	        },
	        fial: (err) => {
	          reject(err)
	        }
	      })
	    })
	  }
}
